package com.znsd.zl.redis.service.impl;

import com.znsd.zl.redis.service.IRedisService;
import com.znsd.zl.redis.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @ClassName RedisServiceImpl
 * @Description Redis缓存业务实现类
 * @Author jaypdd
 * @Date 2020/8/12 23:03
 * @Version v1.0
 */
@Slf4j
@Service("redisService")
public class RedisServiceImpl implements IRedisService {

	@Resource
	private RedisUtils redisUtils;

	/**
	 * 添加普通key-value缓存，time为null则采用默认缓存时间
	 * @author jaypdd
	 * @param key       键
	 * @param value     值
	 * @param time      单位（秒）过期时间，如果time小于0为永久期限
	 * @return 存入缓存成功 return true or false
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean setKeyValue(String key, Object value, Long time) {
		log.info("普通set缓存存入key：{}，value：{}，time:{}", key, value, time);
		if ( time == null ) {
			return redisUtils.set(key, value);
		}
		return redisUtils.set(key, value, time);
	}

	/**
	 * @author jaypdd
	 * @param key   键
	 * @return 值
	 */
	@Override
	public Object getValueByKey(String key) {
		return redisUtils.get(key);
	}
}
